package com.gujiangbo.schedule;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.google.gson.Gson;
import com.gujiangbo.entity.Schoolmate;
import com.gujiangbo.request.BatchGenerateCondition;
import com.gujiangbo.service.SchoolmateService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: gujiangbo.co
 * @Contact: 646832012@qq.com
 * @Date: 2022/1/18 15:04
 * @Version: 1.0
 * @Description:
 */
@Component
@Slf4j
public class XxJobForSchoolMatesTask {

    @Resource
    private SchoolmateService schoolmateService;

    private Gson gson = new Gson();

    @XxlJob(value = "productSchoolMates")
    public ReturnT<String> productSchoolMates() {
        String param = XxlJobHelper.getJobParam();
        log.info(">>>>>>>>>>>>>>>>>" + DateUtil.now() + " param: " + param + ">>>>>>>>>>>>执行");
        BatchGenerateCondition condition = new BatchGenerateCondition();
        if (StrUtil.isBlank(param)) {
            return ReturnT.FAIL;
        }
        Map<String, String> map = gson.fromJson(param, Map.class);
        int batchNo;
        String val = map.get("batchNo");
        if (StrUtil.isBlank(val)) {
            batchNo = 5;
        } else {
            batchNo = Integer.parseInt(val);
        }
        condition.setBatchNo(batchNo);
        schoolmateService.batchGenerateSchoolmates(condition);
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "querySchoolMates")
    public String querySchoolMates() {
        String param = XxlJobHelper.getJobParam();
        log.info(">>>>>>querySchoolMates job params:" + param);
        String resultMsg;
        if (StringUtils.isBlank(param)) return "param is null";
        HashMap<String, String> map = gson.fromJson(param, HashMap.class);
        String id = map.get("ID");
        if (StringUtils.isBlank(id)) {
            return "param is valid";
        }
        Schoolmate schoolmate = schoolmateService.querySchoolmatesLikeByID(id);
        resultMsg = gson.toJson(schoolmate);
        log.info(">>>>>>>>querySchoolMates result:" + resultMsg);
        XxlJobHelper.handleResult(200, resultMsg);
        return resultMsg;
    }

}
